/*******************************************************************************
 * Copyright 2011 Joachim Ansorg, mail@ansorg-it.com
 * File: BashFacetUI.java, Class: BashFacetUI
 * Last modified: 2011-04-30 16:33
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.ansorgit.plugins.bash.settings.facet.ui;

import com.ansorgit.plugins.bash.settings.facet.BashFacetConfiguration;
import com.intellij.facet.ui.FacetEditorContext;
import com.intellij.facet.ui.FacetEditorTab;
import com.intellij.facet.ui.FacetValidatorsManager;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import org.jetbrains.annotations.Nls;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.util.HashMap;

/**
 * GUI settings which are displayed for a Bash module facet.
 * <p/>
 * User: jansorg
 * Date: Feb 11, 2010
 * Time: 10:21:10 PM
 */
public class BashFacetUI extends FacetEditorTab {
    private JRadioButton ignoreFilesWithoutExtensionRadioButton;
    private JRadioButton acceptAllFilesWithoutRadioButton;
    private JRadioButton customSettingsRadioButton;
    private JPanel basePanel;
    private JScrollPane treeScollArea;

    private ModuleFileTreeTable fileTreeTable;

    private final BashFacetConfiguration facetConfiguration;
    private final FacetEditorContext facetEditorContext;

    public BashFacetUI(BashFacetConfiguration facetConfiguration, FacetEditorContext facetEditorContext, FacetValidatorsManager facetValidatorsManager) {
        this.facetConfiguration = facetConfiguration;
        this.facetEditorContext = facetEditorContext;
    }

    @Nls
    public String getDisplayName() {
        return "BashSupport";
    }

    private BashFacetConfiguration.OperationMode findMode() {
        if (ignoreFilesWithoutExtensionRadioButton.isSelected()) {
            return BashFacetConfiguration.OperationMode.IgnoreAll;
        }

        if (acceptAllFilesWithoutRadioButton.isSelected()) {
            return BashFacetConfiguration.OperationMode.AcceptAll;
        }

        return BashFacetConfiguration.OperationMode.Custom;
    }

    private void setMode(BashFacetConfiguration.OperationMode mode) {
        acceptAllFilesWithoutRadioButton.setSelected(mode == BashFacetConfiguration.OperationMode.AcceptAll);
        ignoreFilesWithoutExtensionRadioButton.setSelected(mode == BashFacetConfiguration.OperationMode.IgnoreAll);
        customSettingsRadioButton.setSelected(mode == BashFacetConfiguration.OperationMode.Custom);

        basePanel.setEnabled(customSettingsRadioButton.isSelected());
        fileTreeTable.setEnabled(customSettingsRadioButton.isSelected());
    }

    public JComponent createComponent() {
        customSettingsRadioButton.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JRadioButton button = (JRadioButton) e.getSource();
                treeScollArea.setEnabled(button.isSelected());

                basePanel.setEnabled(customSettingsRadioButton.isSelected());
                fileTreeTable.setEnabled(customSettingsRadioButton.isSelected());
            }
        });

        fileTreeTable = new ModuleFileTreeTable(facetEditorContext.getModule(), new HashMap<VirtualFile, FileMode>(facetConfiguration.getMapping()));
        treeScollArea.setViewportView(fileTreeTable);

        reset();

        return basePanel;
    }

    public boolean isModified() {
        return findMode() != facetConfiguration.getOperationMode() ||
                !fileTreeTable.getMapping().equals(facetConfiguration.getMapping());
    }

    public void apply() throws ConfigurationException {
        facetConfiguration.setOperationMode(findMode());
        facetConfiguration.setMapping(fileTreeTable.getMapping());
    }

    public void reset() {
        setMode(facetConfiguration.getOperationMode());
        fileTreeTable.reset(facetConfiguration.getMapping());
    }

    public void disposeUIResources() {
    }

    private void createUIComponents() {
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        basePanel = new JPanel();
        basePanel.setLayout(new GridLayoutManager(5, 1, new Insets(0, 0, 0, 0), -1, -1));
        final JLabel label1 = new JLabel();
        label1.setText("Here you can configure how BashSupport treats files without extensions.");
        basePanel.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        ignoreFilesWithoutExtensionRadioButton = new JRadioButton();
        ignoreFilesWithoutExtensionRadioButton.setSelected(true);
        ignoreFilesWithoutExtensionRadioButton.setText("Ignore files without extension");
        basePanel.add(ignoreFilesWithoutExtensionRadioButton, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        customSettingsRadioButton = new JRadioButton();
        customSettingsRadioButton.setText("Custom settings");
        basePanel.add(customSettingsRadioButton, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        acceptAllFilesWithoutRadioButton = new JRadioButton();
        acceptAllFilesWithoutRadioButton.setText("Accept all files without extension");
        basePanel.add(acceptAllFilesWithoutRadioButton, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        treeScollArea = new JScrollPane();
        treeScollArea.setEnabled(true);
        basePanel.add(treeScollArea, new GridConstraints(4, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, new Dimension(100, 200), null, null, 0, false));
        ButtonGroup buttonGroup;
        buttonGroup = new ButtonGroup();
        buttonGroup.add(ignoreFilesWithoutExtensionRadioButton);
        buttonGroup.add(acceptAllFilesWithoutRadioButton);
        buttonGroup.add(customSettingsRadioButton);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return basePanel;
    }
}
